package com.vnp.vchat.adapter;

import java.util.ArrayList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.telpoo.frame.utils.Mlog;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.util.Utils;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SearchAdapter extends ArrayAdapter<ObjKhachhang> {

	Context context;
	ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>();
	DisplayImageOptions options = new DisplayImageOptions.Builder()
						.cacheInMemory(false)
						.cacheOnDisc(true)
						.showImageForEmptyUri(R.drawable.defaul_avatar)
						.showImageOnFail(R.drawable.defaul_avatar)
						.bitmapConfig(Config.RGB_565)
						.displayer(new FadeInBitmapDisplayer(200, true, true, true))
						
						.build();

	public SearchAdapter(Context context, ArrayList<ObjKhachhang> objKhachhangs) {
		super(context, R.layout.item_search_result, objKhachhangs);
		this.context = context;
		this.objKhachhangs = objKhachhangs;
	}

	@Override
	public int getCount() {
		return objKhachhangs.size();
	}

	@Override
	public ObjKhachhang getItem(int position) {
		return objKhachhangs.get(position);
		
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v==null){
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.item_search_result, parent, false);
			holder = new Holder();
			
			holder.imgAvatar = (MyCircleImageView) v.findViewById(R.id.img_avatar);
			holder.txtUseName = (TextView) v.findViewById(R.id.use_name);
			holder.txtLoginName = (TextView) v.findViewById(R.id.login_name);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		holder.txtUseName.setText(objKhachhangs.get(position).getName());
		holder.txtUseName.setTypeface(Utils.getTypefaceRegular(context));
		holder.txtLoginName.setText(objKhachhangs.get(position).getLoginName());
		holder.txtLoginName.setTypeface(Utils.getTypefaceRegular(context));
		holder.imgAvatar.setImageResource(R.drawable.defaul_avatar);	
		ImageLoader.getInstance().displayImage(objKhachhangs.get(position).getUrlAvatar(), holder.imgAvatar, options);
		
		return v;
	}
	
	class Holder{
		MyCircleImageView imgAvatar;
		TextView txtUseName;
		TextView txtLoginName;
	}
	
	public void setItems(ArrayList<ObjKhachhang> ojs){
		clear();
		adds(ojs);
	}

	private void adds(ArrayList<ObjKhachhang> ojs) {
		if(ojs==null){
			Mlog.E("adds SearchAdapter null");
			return;
		}
		for (ObjKhachhang objKhachhang : ojs) {
			add(objKhachhang);
		}
	}
	
	public ArrayList<ObjKhachhang> getAll(){
		return objKhachhangs;
	}

}
